$(function () {
    //点击修改按钮
    $("#customer_info_table").on('click','.change-item',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        setInfoModal(node);
        $("#customer-change").modal('show');
    });

    //点击删除按钮
    $("#customer_info_table").on('click','.delete-item',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        //执行删除操作
        deleteCustomerInfo(node);
    });

    //点击重置密码
    $("#customer_info_table").on('click','.reset-item',function () {
        //设置文件信息编辑框
        var node=$(this).parent().parent();
        //将密码重置为123456
        resetCustomerPassword(node);
    });

    //点击搜索按钮
    $("#search_customer").click(function () {
        //验证选择类型
        //获取学生信息
        var account=$("#find_search_control input").val();
        getCustomerInfo(account);
    });

    //点击添加乘客
    $("#add-customer").click(function () {
        //清空注册表
        clearCustomerRegisterModal();
        $("#customer-register").modal('show');
    });
    //提交注册信息
    $("#customer-register-finish").click(function () {
        registerCustomer();
    });
    //提交修改信息
    $("#customer-change-finish").click(function () {
        submitChangeInfo();
    });

});

//设置乘客编辑信息
function setInfoModal(node) {
    var tdArr=node.children();
    $("#customer-name").val(tdArr.eq(0).text());
    $("#customer-phone").val(tdArr.eq(3).text());
    $("#disabled_time").val(tdArr.eq(6).text());
    //设置customerId
    $("#customer-change-finish").attr("customer-id",node.attr("customer-id"));
    sessionStorage.setItem("current-customer-info",
        tdArr.eq(0).text()+tdArr.eq(3).text());
}
//清空乘客注册表
function clearCustomerRegisterModal() {
    $("#customer-register-name").val("");
    $("#customer-register-phone").val("");
    $("#customer-register-password1").val("");
    $("#customer-register-password2").val("");
}
//提交修改信息
function submitChangeInfo() {
    var customerId=$("#customer-change-finish").attr("customer-id");
    var name=$("#customer-name").val();
    var phone=$("#customer-phone").val();
    var content=sessionStorage.getItem("current-customer-info");
    if(phone.length!=11){
        alert("手机号格式错误！");
        return;
    }
    if(content==(name+phone)){
        alert("修改成功！");
        $("#changeInfo").modal('hide');
        return;
    }
    var dataInfo={
        name: name,
        phone: phone
    };
    $.ajax({
        type: "POST",
        url: "/manage/change/customer/"+customerId,
        contentType: "application/json",
        data: JSON.stringify(dataInfo),
        success: function (result) {
            if(result==0){
                alert("修改成功！");
                $("#customer-change").modal('hide');
            }else{
                alert("修改失败！");
            }
        }
    });
}
//删除乘客信息
function deleteCustomerInfo(node) {
    var customerId=node.attr("customer-id");
    $.ajax({
        type: "GET",
        url: "/manage/delete/customer/"+customerId,
        Node: node,
        success: function (result) {
            if(result==0){
                alert("删除成功！");
                //移除当前元素
                this.Node.remove();
            }else{
                alert("删除失败！");
            }

        }
    });
}
//重置密码
function resetCustomerPassword(node) {
    var customerId=node.attr("customer-id");
    $.ajax({
        type: "GET",
        url: "/manage/reset/customer/"+customerId+"/password",
        Node: node,
        success: function (result) {
            if(result==0){
                alert("重置成功！");
                //修改前台显示密码
                var tdArr=this.Node.children();
                tdArr.eq(2).text("123456");
            }else{
                alert("重置失败！");
            }

        }
    });

}
//通过手机号获取乘客信息
function getCustomerInfo(account) {
    if(account.length!=11){
        alert("手机号格式错误！");
        return;
    }
    $.ajax({
        type: "GET",
        url: "/customer/get/info/by/"+account,
        success: function (result) {
            if(result.status==0){
                //获取成功
                var file=result.customer;
                var parent=$("#customer_info_table");
                createCustomerTable(file,parent);
            }else{
                //获取失败
                alert("信息获取失败，手机号不存在或输入错误！");
            }
        }
    });
}
//清空乘客信息表
function clearCustomerTable() {
    $("#customer_info_table tr:not(:first)").empty();
}
//创建乘客信息表
function createCustomerTable(file,parent) {
    clearCustomerTable();
    var customerId=file.customerId;
    var account=file.account;
    var name=file.customerName;
    var password=file.passWord;
    var phone=file.phone;
    var orders=file.finishedOrders;
    var createTime=getFormattedDateTime(file.createTime);
    var money=file.money;
    //添加
    var node=$('<tr></tr>');
    node.attr("customer-id",customerId);
    node.append('<td>'+name+'</td>');
    node.append('<td>'+account+'</td>');
    node.append('<td>'+password+'</td>');
    node.append('<td>'+phone+'</td>');
    node.append('<td>'+money+'</td>');
    node.append('<td>'+orders+'</td>');
    node.append('<td>'+createTime+'</td>');
    node.append('<td>\n' +
        '                            <button type="button" class="btn btn-default delete-item">删除</button>\n' +
        '                            <button type="button" class="btn btn-success change-item">修改</button>\n' +
        '                            <button type="button" class="btn btn-info reset-item">重置密码</button>\n' +
        '                            </td>');
    node.appendTo(parent);
}
//添加乘客
function registerCustomer() {
    var name=$("#customer-register-name").val();
    var phone=$("#customer-register-phone").val();
    var password1=$("#customer-register-password1").val();
    var password2=$("#customer-register-password2").val();
    if(name.length==0){
        alert("姓名为空！");
        return;
    }
    if(phone.length!=11){
        alert("手机号格式错误！");
        return;
    }
    if(password1.length<6){
        alert("密码长度小于6！");
        return;
    }
    if(password1!=password2){
        alert("两次输入的密码不一致！");
        return;
    }
    //提交注册信息
    var dataInfo={
        customerName: name,
        phone: phone,
        passWord: password1
    };
    $.ajax({
        type: "POST",
        url: "/customer/register",
        contentType: "application/json",
        data: JSON.stringify(dataInfo),
        success: function (result) {
            if(result==0){
                alert("注册成功！");
                //关闭模态框
                $("#customer-register").modal('hide');
            }else if(result==1){
                alert("注册失败,电话号码重复！");
            }else{
                alert("注册失败,未知原因！");
            }
        }
    });
}